import java.util.List;

/*
 * @author zzr
 * @date: 2025/10/12  13:29
 * @description:
 */
public class MyLinkedList {
    static class Node{
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }
    public Node head;
    public int size;

    public void createList() {
        Node node1 = new Node(12);
        Node node2 = new Node(23);
        Node node3 = new Node(34);
        Node node4 = new Node(45);
        Node node5 = new Node(56);
        Node node6 = new Node(67);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;

        head = node1;
    }

    /**
     * 打印链表
     */
    public void display() {
        Node cur = head;
        while (cur != null) {
            System.out.println(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 头插法
     * @param data
     */
    public void addFirst(int data) {
        Node node = new Node(data);
        node.next = head;
        head = node;
        size++;
    }

    /**
     * 尾插法
     * @param data
     */
    public void addLast(int data) {
        Node node = new Node(data);

        Node cur = head;

        if (cur == null) {
            head = node;
            return;
        }
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
        size++;
    }

    /**
     * 在指定 index 位置插入数据为 data 的数据
     * @param index
     * @param data
     */
    public void addIndex(int index, int data) {
        Node cur = head;
        Node node = new Node(data);

        if (index < 0 || index > size) {
            System.out.println("index 不合法");
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size) {
            addLast(data);
            return;
        }
        // 找到要添加位置的前一个位置
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
    }

    /**
     * 找到 val 为 key 的节点的前一个节点
     * @param key
     * @return
     */
    public Node searchPrev(int key) {
        Node cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    /**
     * 删除值为 key 的元素
     * @param key
     */
    public void remove(int key) {

        if (head == null) {
            return;
        }
        if (head.val == key) {
            head = head.next;
            return;
        }
        Node cur = searchPrev(key);
        if (cur == null) {
            System.out.println("没有要删除的数据");
            return;
        }
        Node del = cur.next;
        cur.next = del.next;
    }

    /**
     * 删除所有元素为 key 的节点
     * @param key
     */
    public void removeAll(int key) {
        if (head == null) {
            return;
        }
        Node prev = head;
        Node cur = head.next;

        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next; // 删除元素
            } else {
                prev = cur;
            }
            cur = cur.next;
        }
        if (head.val == key) {
            head = head.next;
        }
    }
}
